import dataconver from './data_conversiontest'
let serialNo = 0;

function getDataByNum(data) {
  const dataAnalysis = {
    packageCode: parseInt(data.substring(0, 4), 16),
    packageNumber: parseInt(data.substring(4, 8), 16),
    packageSize: parseInt(data.substring(8, 12), 16),
    sumdPackageSize: parseInt(data.substring(12, 20), 16),
    subpackageNumber: parseInt(data.substring(20, 24), 16),
    commandCode: parseInt(data.substring(24, 28), 16),
    dataAll: data.substring(28, data.length - 4),
    datacheck: parseInt(data.length - 4, 16)
  }
  return dataAnalysis;
}

//根据命令码查询各项数据
function getNumBycommand(commandCode, dataDetail) {
  //判断命令码是什么
  switch (commandCode) {
    case 1:
      let switchState = dataDetail
      return switchState;
    case 2:
      let lightSwitch = dataDetail
      return lightSwitch;
    case 3:
      let freeSwitch = dataDetail
      return freeSwitch;
    case 4:
      let storageSwitch = dataDetail
      return storageSwitch;
    case 5:
      let xf1Switch = dataDetail
      return xf1Switch;
    case 6:
      let xf2Switch = dataDetail
      return xf2Switch;
    case 7:
      //获取预约状态
      let scheduleSunday = dataDetail[0]
      let scheduleMonday = dataDetail[1]
      let scheduleTuesday = dataDetail[2]
      let scheduleWednesday = dataDetail[3]
      let scheduleThursday = dataDetail[4]
      let scheduleFriday = dataDetail[5]
      let scheduleSaturday = dataDetail[6]
      return {
        scheduleSunday: scheduleSunday, scheduleMonday: scheduleMonday, scheduleTuesday: scheduleTuesday,
        scheduleWednesday: scheduleWednesday, scheduleThursday: scheduleThursday,
        scheduleFriday: scheduleFriday, scheduleSaturday: scheduleSaturday
      }
    case 8:
      let autoSwitch = dataDetail
      return autoSwitch;
    case 9:
      //获取继电器输出状态
      let heatTube = (dataDetail.RelS & (1 << 0)) == (1 << 0) ? 1 : 0
      let compressorOutput = (dataDetail.RelS & (1 << 1)) == (1 << 1) ? 1 : 0
      let spoutOutput = (dataDetail.RelS & (1 << 2)) == (1 << 2) ? 1 : 0
      let light = (dataDetail.RelS & (1 << 3)) == (1 << 3) ? 1 : 0
      let defrostOutput = (dataDetail.RelS & (1 << 4)) == (1 << 4) ? 1 : 0
      let fanOutput = (dataDetail.RelS & (1 << 5)) == (1 << 5) ? 1 : 0
      let steamHeatOutput = (dataDetail.RelS & (1 << 6)) == (1 << 6) ? 1 : 0
      let auxiliaryOutput = (dataDetail.RelS & (1 << 7)) == (1 << 7) ? 1 : 0
      //返回错误状态和最低电压
      let tempAndHumErro = (dataDetail.ErrCode & (1 << 0)) == (1 << 0) ? 1 : 0
      let defrostThermErro = (dataDetail.ErrCode & (1 << 1)) == (1 << 1) ? 1 : 0
      let tempExceedingErro = (dataDetail.ErrCode & (1 << 2)) == (1 << 2) ? 1 : 0
      let waterShortageErro = (dataDetail.ErrCode & (1 << 3)) == (1 << 3) ? 1 : 0
      let freezingAndreErro = (dataDetail.ErrCode & (1 << 4)) == (1 << 4) ? 1 : 0
      //返回预约状态
      let scheduleSundayState = (dataDetail.OrdS & (1 << 0)) == (1 << 0) ? 1 : 0
      let scheduleMondayState = (dataDetail.OrdS & (1 << 1)) == (1 << 1) ? 1 : 0
      let scheduleTuesdayState = (dataDetail.OrdS & (1 << 2)) == (1 << 2) ? 1 : 0
      let scheduleWednesdayState = (dataDetail.OrdS & (1 << 3)) == (1 << 3) ? 1 : 0
      let scheduleThursdayState = (dataDetail.OrdS & (1 << 4)) == (1 << 4) ? 1 : 0
      let scheduleFridayState = (dataDetail.OrdS & (1 << 5)) == (1 << 5) ? 1 : 0
      let scheduleSaturdayState = (dataDetail.OrdS & (1 << 6)) == (1 << 6) ? 1 : 0
      return {
        compTemp: dataDetail.ComTp,
        currTemp: dataDetail.CurrTp,
        currHumidity: dataDetail.CurrHm,
        minvoltage: dataDetail.LowV,
        defrostingTemp: dataDetail.DefTp,
        currVoltage: dataDetail.CurrV,
        currMode: dataconver.getHeight4(dataDetail.RunM),
        currState: dataconver.getLow4(dataDetail.RunM),
        heatTube: heatTube, compressorOutput: compressorOutput,
        spoutOutput: spoutOutput, light: light, defrostOutput: defrostOutput,
        fanOutput: fanOutput, steamHeatOutput: steamHeatOutput, auxiliaryOutput: auxiliaryOutput,
        tempAndHumErro: tempAndHumErro, defrostThermErro: defrostThermErro,
        tempExceedingErro: tempExceedingErro, waterShortageErro:
          waterShortageErro, freezingAndreErro: freezingAndreErro,
        weekReservation: {
          scheduleSundayState: scheduleSundayState,
          scheduleMondayState: scheduleMondayState,
          scheduleTuesdayState: scheduleTuesdayState,
          scheduleWednesdayState: scheduleWednesdayState,
          scheduleThursdayState: scheduleThursdayState,
          scheduleFridayState: scheduleFridayState,
          scheduleSaturdayState: scheduleSaturdayState
        },
        controlPanelVesion: (dataDetail.FirmVer >> 8 & 0x0F) + '.' + (dataDetail.FirmVer >> 4 & 0x00F) + "." + (dataDetail.FirmVer & 0x000F),
        upperComputerVesion: (dataDetail.SoftVer >> 6) + '.' + (dataDetail.SoftVer >> 4 & 0x03) + '.' + (dataDetail.SoftVer & 0x0f),
        communicationState: dataDetail.CommSt,
        connt: dataDetail.Connt
      }
    case 10:
      //设备当前时间
      let currYear = dataDetail.Year
      let currMonth = dataDetail.Month
      let currDay = dataDetail.Day
      let currWeek = dataDetail.Week
      let currTime = dataDetail.Hour
      let currMin = dataDetail.Minute
      return {
        currYear: currYear, currMonth: currMonth, currDay: currDay, currWeek: currWeek, currTime: currTime, currMin: currMin
      }
    case 11:
      let chiT = (dataDetail.ChiT).toString(16);
      let prf1T = (dataDetail.Prf1T).toString(16);
      let prf2ST = (dataDetail.Prf2ST).toString(16);
      let prf2ET = (dataDetail.Prf2ET).toString(16);
      let freeTemp = dataDetail.FreTp
      let storageTemp = dataDetail.ChiTp
      let xf1Temp = dataDetail.Prf1Tp
      let xf2Temp = dataDetail.Prf2Tp
      let xf1Humidity = dataDetail.Prf1Hm
      let xf2Humidity = dataDetail.Prf2Hm
      return {
        refrigerateSTime: chiT,
        xf1STime: prf1T,
        xf2STime: prf2ST,
        xf2ETime: prf2ET,
        freeTemp: freeTemp, storageTemp: storageTemp, xf1Temp: xf1Temp,
        xf2Temp: xf2Temp, xf1Humidity: xf1Humidity, xf2Humidity: xf2Humidity
      }
    case 12:
      let freeAndStorageHighTemp = dataDetail.HTp
      let freeAndStorageLowTemp = dataDetail.LTp
      let tempCorrect = dataDetail.TpCor
      let tempControlDifference = dataDetail.TpCon
      let tempUpConvergence = dataDetail.TpUp
      let tempDownConvergence = dataDetail.TpDw
      let steamMode = dataDetail.Steam
      let lightDelay = dataDetail.Light
      let proofWeek = dataDetail.ProCyc
      let proofHeatTime = dataDetail.ProHt
      let proofHighTemp = dataDetail.ProHTp
      let proofLowTemp = dataDetail.ProLTp
      let keyTone = dataDetail.Buzz
      let humidityCheck = dataDetail.HmCor
      let steamEtime = dataDetail.SpONT
      let steamStime = dataDetail.SpOFFT
      let humidityDifference = dataDetail.HmDiff
      let humidityUpConvergence = dataDetail.HmUp
      let humidityDownConvergence = dataDetail.HmDw
      let defrostTime = dataDetail.DefT
      let defrostWeek = dataDetail.DefCyc
      let defrostSTemp = dataDetail.DefSTp
      let defrostETemp = dataDetail.DefETp
      let defrostFanMode = dataDetail.DefFen
      let compressorLongRunTime = dataDetail.ComRunT
      let compressorLowTempTime = dataDetail.ComColT
      let compressorFiringDelayed = dataDetail.ComDey
      let displayLight = dataDetail.Bright
      let highTemperatureAlarm = dataDetail.HTpWarn
      let curveEMode = dataDetail.CurCon
      let humidityControlCheck = dataDetail.HmCon
      let proofOverTemp = dataDetail.ComSTp
      let atomizingHeatSTime = dataDetail.HtONT
      let atomizingHeatETime = dataDetail.HtOFFT
      let humidityReachAtomizingTime = dataDetail.SpT
      let fanSTime = dataDetail.FenONT
      let fanETime = dataDetail.FenOFFT
      let heatMethod = dataDetail.HtMode
      let relWork = dataDetail.RelWork
      return {
        freeAndStorageHighTemp: freeAndStorageHighTemp, freeAndStorageLowTemp: freeAndStorageLowTemp,
        tempCorrect: tempCorrect, tempControlDifference: tempControlDifference, tempUpConvergence:
          tempUpConvergence, tempDownConvergence: tempDownConvergence, steamMode: steamMode,
        lightDelay: lightDelay,
        proofWeek: proofWeek, proofHeatTime: proofHeatTime, proofHighTemp: proofHighTemp, proofLowTemp:
          proofLowTemp, keyTone: keyTone,
        humidityCheck: humidityCheck, steamEtime: steamEtime, steamStime: steamStime, humidityDifference:
          humidityDifference, humidityUpConvergence: humidityUpConvergence,
        humidityDownConvergence: humidityDownConvergence,
        defrostTime: defrostTime, defrostWeek: defrostWeek, defrostSTemp: defrostSTemp,
        defrostETemp: defrostETemp, defrostFanMode: defrostFanMode,
        compressorLongRunTime: compressorLongRunTime, compressorLowTempTime: compressorLowTempTime,
        compressorFiringDelayed: compressorFiringDelayed, displayLight: displayLight,
        highTemperatureAlarm: highTemperatureAlarm, curveEMode: curveEMode,
        humidityControlCheck: humidityControlCheck, proofOverTemp: proofOverTemp,
        atomizingHeatSTime: atomizingHeatSTime, atomizingHeatETime: atomizingHeatETime,
        humidityReachAtomizingTime: humidityReachAtomizingTime, fanSTime: fanSTime, fanETime: fanETime,
        heatMethod: heatMethod,
        relWork: relWork
      }
    case 14:
      //返回升级状态数据
      return {
        Type: dataDetail.Type,
        Version: dataDetail.Version,
        Stage: dataDetail.Stage,
        Progress: dataDetail.Progress,
        Result: dataDetail.Result
      }
  }
}

//下发数据解析
function combineCommand(cmdType, data, num) {
  let dataPackSerialNo = ((serialNo++) % 0xefff)
  let version = 0;
  let device_type = 1;
  let deviceUniqueNo = 0;
  let send_cmd = 1;
  let comData;
  if (num == 1) {
    comData = data101(data)
  } else if (num == 2) {
    comData = data102(data)
  } else if (num == 3) {
    comData = data103(data)
  } else {
    comData = data
  }
  let dataCommand = dataconver.Bytes2Str(dataconver.int22Byte(dataPackSerialNo, 0, true)) + dataconver.toHex(version) +
    dataconver.Bytes2Str(dataconver.int24Byte(device_type, false)) + dataconver.Bytes2Str(dataconver.int24Byte(
      deviceUniqueNo, false)) + dataconver.toHex(send_cmd) + dataconver.toHex(cmdType) + dataconver.Bytes2Str(comData)
  let dataCrc = dataconver.crc16_false(dataCommand)
  dataCommand = dataCommand + dataCrc
  return dataCommand;
}
//数据101解析
function data101(data) {
  let defaultData = new Array(8);
  if (data == null) {
    data = defaultData
  } else if (data.length < 8) {
    //将data数据填充到defaultData 不够八位补0
    for (var i = 0; i < 8; i++) {
      if (i < data.length) {
        defaultData[i] = data[i];
      } else {
        defaultData[i] = 0;
      }
    }
    data = defaultData;
  } else if (data.length > 8) {
    this.$modal.msgError("数据不正确");
  }
  return data;
}

function data102(data102) {
  let defaultData102 = new Array(32);
  if (data102 == null) {
    data102 = defaultData102
  } else if (data102.length < 32) {
    //将data数据填充到defaultData 不够32位补0
    for (var i = 0; i < 32; i++) {
      if (i < data102.length) {
        defaultData102[i] = data102[i];
      } else {
        defaultData102[i] = 0;
      }
    }
    data102 = defaultData102;
  } else if (data102.length > 32) {
    this.$modal.msgError("数据不正确");
  }
  return data102;
}

function data103(data103) {
  let defaultData103 = new Array(64);
  if (data103 == null) {
    data103 = defaultData103
  } else if (data103.length < 64) {
    //将data数据填充到defaultData 不够64位补0
    for (var i = 0; i < 64; i++) {
      if (i < data103.length) {
        defaultData103[i] = data103[i];
      } else {
        defaultData103[i] = 0;
      }
    }
    data103 = defaultData103;
  } else if (data103.length > 32) {
    this.$modal.msgError("数据不正确");
  }
  return data103;
}

// 我写的十进制转换成二进制方法
function tentotwo(decimalNumber) {
  let binaryString = decimalNumber.toString(2).padStart(8, '0');
  // 打印二进制字符串到控制台
  console.log("二进制数据:", binaryString);
  // 将二进制字符串转换为布尔值数组，并反转它
  let reversedBooleanArray = Array.from(binaryString)
    .reverse()
    .map(bit => bit === '1');
  // 如果需要将布尔值数组组合成一个新的表示形式，可以这样做
  let booleanRepresentation = reversedBooleanArray.map(b => b ? true : false)
  return booleanRepresentation;
}

function getAllBits(num, index) {
  return (num & (1 << index)) == (1 << index);
}
export default {
  getNumBycommand,
  getDataByNum,
  combineCommand,
  tentotwo,
  getAllBits
}